﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Maticsoft.Common;
using System.Web.SessionState;

namespace Maticsoft.Web.ashx
{
    /// <summary>
    /// sendmsg 的摘要说明
    /// </summary>
    public class sendmsg : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string mobile = context.Request.QueryString["mobile"];
            string op = context.Request.QueryString["op"];
            string verify = context.Request.QueryString["verifycode"];
            try
            {
                if (!string.IsNullOrEmpty(mobile) && PageValidate.IsMobile(mobile))
                {
                    if (!string.IsNullOrEmpty(op) && op == "reg")
                    {
                        BLL.Members meBLL = new BLL.Members();
                        Model.Members meModel = new Model.Members();
                        meModel = meBLL.GetModel(mobile);
                        if (meModel != null)
                        {
                            context.Response.Write("-3");
                            return;
                        }
                    }

                    if (HttpContext.Current.Session["niudunwu"] == null || HttpContext.Current.Session["niudunwu"].ToString() != verify)
                    {
                        context.Response.Write("-4");
                        return;
                    }

                    BLL.SendMsg msgBLL = new BLL.SendMsg();
                    Model.SendMsg msgModel = new Model.SendMsg();
                    Assistant assistant = new Assistant();
                    string verifyCode = assistant.GetRandomCode("1,2,3,4,5,6,7,8,9", 4);
                    MsgHelper msgHelper = new MsgHelper();
                    string msgContent = "您的验证码为" + verifyCode;
                    if (msgHelper.SendMsg(mobile, msgContent))
                    {
                        msgModel.Mobile = mobile;
                        msgModel.VerifyCode = verifyCode;
                        msgModel.MsgContent = msgContent;
                        msgModel.CreateTime = DateTime.Now;
                        msgModel.Status = 1;
                        msgBLL.Add(msgModel);

                        context.Response.Write("1");
                    }
                    else
                    {
                        context.Response.Write("-2");
                    }
                }
                else
                {
                    context.Response.Write("-1");
                }
            }
            catch
            {
                context.Response.Write("0");
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}